﻿Imports System.IO

Public Class GridComponentes
    Inherits System.Web.UI.UserControl

    Public ControlesDesplegados As Hashtable

    Public Sub CargarComponentes(ByVal CodEscandallo As String)

        GVComponentes.DataSource = LeerTablaComponentes(CodEscandallo)
        GVComponentes.DataBind()

        GVComponentes.CssClass = "tabla"
        GVComponentes.AlternatingRowStyle.CssClass = "filatablaalt"
        GVComponentes.HeaderRow.CssClass = "encabezadotabla"

    End Sub

    Public Sub RecargarControlesDinamicos()
        Dim Fila As GridViewRow
        For Each Fila In GVComponentes.Rows
            If ControlesDesplegados.ContainsKey(GVComponentes.ID & Fila.Cells(1).Text) Then
                Dim TablaComponentes As DataTable = LeerTablaComponentes(Fila.Cells(1).Text)
                If TablaComponentes.Rows.Count > 0 Then
                    CrearGridComponentes(GVComponentes, Fila, Fila.Cells(1).Text)
                End If
            End If
        Next
    End Sub

    Public Sub GVComponentes_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
        Select Case e.CommandName
            Case "verescandallo"
                Dim Grid As GridView = sender
                If ControlesDesplegados.ContainsKey(Grid.ID & Grid.Rows(CInt(e.CommandArgument)).Cells(1).Text) Then
                    Grid.Rows(CInt(e.CommandArgument)).Cells(6).Visible = False
                    Grid.Rows(CInt(e.CommandArgument)).Cells(7).Visible = False
                    Grid.Rows(CInt(e.CommandArgument)).Cells(8).Visible = False
                    ControlesDesplegados.Remove(Grid.ID & Grid.Rows(CInt(e.CommandArgument)).Cells(1).Text)
                Else
                    ControlesDesplegados.Add(Grid.ID & Grid.Rows(CInt(e.CommandArgument)).Cells(1).Text, Nothing)
                    CrearGridComponentes(Grid, Grid.Rows(CInt(e.CommandArgument)), Grid.Rows(CInt(e.CommandArgument)).Cells(1).Text)
                End If
        End Select

    End Sub

    Public Sub GVComponentes_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        e.Row.Cells(6).Visible = False
        e.Row.Cells(7).Visible = False
        e.Row.Cells(8).Visible = False
        Dim TablaComponentes As DataTable = LeerTablaComponentes(e.Row.Cells(1).Text)
        If TablaComponentes.Rows.Count > 0 Then
            e.Row.Cells(5).Visible = True
            Dim Grid As GridView = sender
            If ControlesDesplegados.ContainsKey(Grid.ID & e.Row.Cells(1).Text) Then
                CrearGridComponentes(sender, e.Row, e.Row.Cells(1).Text)
            End If
        Else
            e.Row.Cells(5).Visible = False
        End If

    End Sub

    Public Sub CrearGridComponentes(ByRef Grid As GridView, ByRef FilaContenedor As GridViewRow, ByVal CodArticulo As String)
        FilaContenedor.Cells(6).Visible = True
        FilaContenedor.Cells(7).Visible = True
        FilaContenedor.Cells(8).Visible = True
        FilaContenedor.Cells(7).ColumnSpan = 5

        Dim NuevoGrid As Facody.GridComponentes
        NuevoGrid = CType(LoadControl("~\controles\GridComponentes.ascx"), Facody.GridComponentes)
        NuevoGrid.ID = Grid.ID & "GV" & CodArticulo
        NuevoGrid.ControlesDesplegados = ControlesDesplegados
        NuevoGrid.CargarComponentes(CodArticulo)

        FilaContenedor.Cells(7).Controls.Add(NuevoGrid)

    End Sub

    Private Function LeerTablaComponentes(ByVal CodArticulo As String) As DataTable
        Dim SQL As String
        SQL = "SELECT F_ARTICLE.AR_REF, F_ARTICLE.AR_DESIGN, NO_Qte, AS_QteSto " _
            & " FROM F_NOMENCLAT " _
            & " LEFT JOIN F_ARTICLE ON F_NOMENCLAT.NO_RefDet = F_ARTICLE.AR_REF " _
            & " LEFT JOIN F_ARTSTOCK ON F_NOMENCLAT.NO_RefDet = F_ARTSTOCK.AR_Ref " _
            & " WHERE F_NOMENCLAT.AR_REF = '" & CodArticulo & "'" _
            & " AND F_ARTSTOCK.DE_No = 1 " _
            & " ORDER BY NO_ORDRE "
        Dim BD As New clsBD
        BD.ConectarSAGE(My.Settings.DSNFacody)
        Dim TablaComponentes As DataTable = BD.cargaTablaSAGE(SQL)
        Return TablaComponentes
    End Function

    Function IconoImagen(ByVal CodArticulo As String) As String

        If File.Exists(Server.MapPath("imgproductos/iconos/" & CodArticulo & ".jpg")) Then
            Return "~/imgproductos/iconos/" & CodArticulo & ".jpg"
        Else
            Return "~/img/blanco.png"
        End If
    End Function


End Class